package com.android.delivery.activity.siji

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.ada.common.util.CollectionUtil
import com.android.delivery.R
import com.android.delivery.adapter.siji.OrderAdapter
import com.android.delivery.api.ApiService
import com.android.delivery.base.BaseActivity
import com.android.delivery.callback.DialogCallback
import com.android.delivery.callback.LzyResponse
import com.android.delivery.constant.Constants
import com.android.delivery.constant.IntentKeyConstant
import com.android.delivery.dialog.NavigationDialog
import com.android.delivery.entity.Logistics
import com.android.delivery.entity.Transport
import com.android.delivery.entity.even.Refresh
import com.android.delivery.util.ToastUtils
import com.blankj.utilcode.util.ObjectUtils
import com.blankj.utilcode.util.PhoneUtils
import com.lzy.okgo.model.Response
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.activity_wait_bus_details.*
import kotlinx.android.synthetic.main.include_no_data.*
import org.greenrobot.eventbus.EventBus

/**
 * @author: mqwang
 * @create: 3/17/21 8:16 AM
 * @Description: 待上车详情
 */
class WaitBusDetailsActivity : BaseActivity() {

    companion object{
        fun self(context: Activity, item: Transport, requestCode:Int){
            val intent = Intent(context, WaitBusDetailsActivity::class.java)
            intent.putExtra(Constants.ITEM, item)
            context.startActivityForResult(intent, requestCode)
        }
    }

    private var item: Transport? = null

    private val haveBusAdapter : OrderAdapter by lazy {
        OrderAdapter()
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_wait_bus_details
    }

    override fun init(savedInstanceState: Bundle?, container: View?) {
        setMainTitle("详情")

        refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener{
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                getData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                onRefresh()
            }
        })

        item = intent.getParcelableExtra<Transport>(Constants.ITEM)

        if (item != null){
            tv_node.setText(item!!.locationName)
            tv_phone.setText(item!!.mobile)
            tv_num.setText(item!!.quantity.toString() + "件")
        }
        recycler_view.adapter = haveBusAdapter

        tv_navigation.setOnClickListener {
        }

        tv_phone.setOnClickListener {
            PhoneUtils.dial("")
        }

        tv_debus.setOnClickListener {
           if (ObjectUtils.isEmpty(getSelectIdList())){
               ToastUtils.showShort(this, "请选择")
               return@setOnClickListener
           }
            ApiService.transport_unload(this, getSelectIdList(), object :DialogCallback<LzyResponse<Any>>(this){
                override fun onSuccess(response: Response<LzyResponse<Any>>?) {
                    ToastUtils.showShort(this@WaitBusDetailsActivity, "操作成功")
                    setResult(Activity.RESULT_OK)
                    onRefresh()
                }
            })
        }

        tv_delivery.setOnClickListener {
            if (ObjectUtils.isEmpty(getSelectIdList())){
                ToastUtils.showShort(this, "请选择")
                return@setOnClickListener
            }
            ApiService.transport_arrive(this, getSelectIdList(), object :DialogCallback<LzyResponse<Any>>(this){
                override fun onSuccess(response: Response<LzyResponse<Any>>?) {
                    ToastUtils.showShort(this@WaitBusDetailsActivity, "操作成功")
                    setResult(Activity.RESULT_OK)
                    onRefresh()
                }
            })
        }

        haveBusAdapter.setOnItemClickListener { adapter, view, position ->
            haveBusAdapter.selectItem(position)
        }

        rb_all.setOnCheckedChangeListener { buttonView, isChecked ->
            for (element in haveBusAdapter.data){
                element.isSelect = isChecked
            }
            haveBusAdapter.notifyDataSetChanged()
        }

        refreshLayout.autoRefresh()
    }

    private fun onRefresh() {
        page = 1
        getData()
    }

    fun getSelectIdList(): MutableList<String>{

        var list :MutableList<String> = ArrayList()
        for (element in haveBusAdapter.data){
            if (element.isSelect){
                list.add(element.logisticsNo)
            }
        }
        return list

    }

    var page : Int = 1
    fun getData(){
        ApiService.transport_item(this, item!!.locationId, page, object : DialogCallback<LzyResponse<Logistics>>(){
            override fun onSuccess(response: Response<LzyResponse<Logistics>>?) {
                val data = response!!.body().data
                tv_num.setText(data.total.toString() + "件")
                onGetDataSuc(data.records)
            }
        })
    }

    /**
     * 拉取数据成功时调用
     * @param data 数据源
     */
    protected fun onGetDataSuc(data: MutableList<Logistics>) {
        finishRefresh(refreshLayout)
        bindData(isRefresh(), data)
        if (!ObjectUtils.isEmpty(data)) {
            page++
        }
        checkData()
        if (CollectionUtil.getSize(data) < IntentKeyConstant.PAGE_COUNT) { //无更多
            refreshLayout.setNoMoreData(true) //将不会再次触发加载更多事件
        } else {
            refreshLayout.setNoMoreData(false) //还有更多
        }
    }

    fun bindData(isRefresh: Boolean, list: MutableList<Logistics>) {
        when (isRefresh) {
            true -> {
                haveBusAdapter.setNewData(list)
            }
            false -> {
                haveBusAdapter.addData(list!!)
            }
        }
    }

    private fun isRefresh(): Boolean {
        return page == 1
    }

    private fun checkData() {
        if (!ObjectUtils.isEmpty(haveBusAdapter.data)) {
            layout_empty.setVisibility(View.GONE)
        } else {
            layout_empty.setVisibility(View.VISIBLE)
        }
    }
}